//
//  BEBaseViewController.swift
//  BEGlobalOiler
//
//  Created by KKING on 16/8/14.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit
import SideMenu


class BEBaseViewController: UIViewController {
    
    private lazy var navc:UISideMenuNavigationController = {
        let rightVC = BESideMenuTableView()
        
        
        rightVC.pushViewControllerBlock = {
            (target: String) -> () in
            
            self.dismiss(animated: true, completion: nil)
            if isLogin() == false {
                return
            }
            
            switch target {
            case "myAccount":
                self.tabBarController?.selectedIndex = 1
                break
            case "logOut":
                self.presentLogoutVc()
                break
            case "payment":
                self.presentVc(target: "payment")
                break
            case "accountInfo":
                self.presentVc(target: "accountInfo")
                break
            case "accountSecurity":
                self.presentVc(target: "accountSecurity")
                break
            case "help":
                self.presentVc(target: "help")
                break
            case "aboutUS":
                self.presentVc(target: "aboutUS")
                break
            default:
                let alertC = UIAlertController(title: "正在研发中...", message: nil, preferredStyle: .alert)
                let action1 = UIAlertAction(title: "OK", style: .destructive) { (action) in
                }
                let action2 = UIAlertAction(title: "Cancle", style: .cancel) { (action) in
                }
                alertC.addAction(action1)
                alertC.addAction(action2)
                self.present(alertC, animated: false, completion: nil)
                break
            }
        }
        let navc = UISideMenuNavigationController(rootViewController:rightVC)
        return navc
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let titImgView = UIImageView(image: UIImage(named: "Global-Oiler"))
        navigationItem.titleView = titImgView
        navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: 62, height: 24)
        setupSideMenu()
        setDefaults()
    }
    
    
    /// 登出Vc
    private func presentLogoutVc(){
//        if isLogin() == false {
//            return
//        }
        let alertC = UIAlertController(title: "确定注销登录？", message: nil, preferredStyle: .actionSheet)
        JFProgressHUD.showWithStatus(status: "Is exiting...")
        let action1 = UIAlertAction(title: "OK", style: .destructive) { (action) in
            JFProgressHUD.showSuccessWithStatus(status: "Exit success!")
            DispatchQueue.main.async(execute: {
                BEAccountModel.logout()
                _ = isLogin()
                _ = self.navigationController?.popViewController(animated: true)
            })
        }
        let action2 = UIAlertAction(title: "Cancle", style: .cancel) { (action) in
            JFProgressHUD.dismiss()
        }
        alertC.addAction(action1)
        alertC.addAction(action2)
        present(alertC, animated: false, completion: nil)
    }
    
    
    private func presentVc(target: String){
        switch target {
        case "payment":
            navigationController?.pushViewController(BEPaymentController(), animated: false)
            break
        case "accountInfo":
            navigationController?.pushViewController(BEAccountInfoVc(), animated: false)
            break
        case "accountSecurity":
            navigationController?.pushViewController(BEAccountSecurityVc(), animated: false)
            break
        case "help":
            navigationController?.pushViewController(BEHelpController(), animated: false)
            break
        case "aboutUS":
            navigationController?.pushViewController(BEAboutUSController(), animated: false)
            break
        default:
            break
        }
    }
    
    private func setupSideMenu(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem.leftItem(normalImage: "leftMenu", highlightedImage: "leftMenu", target: self, action: #selector(leftBtnClick))
        
        //            UIBarButtonItem.leftItem("Menu", target: self, action: #selector(leftBtnClick))
        
        //        (title: "Menu", style: .Plain, target: self, action: #selector(leftBtnClick))
        
        SideMenuManager.menuLeftNavigationController = navc
        SideMenuManager.menuRightNavigationController = nil
        //        SideMenuManager.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)
        //        SideMenuManager.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)
    }
    
    
    
    private func setDefaults() {
        
        SideMenuManager.menuPresentMode = .menuSlideIn
        SideMenuManager.menuBlurEffectStyle = .dark
        SideMenuManager.menuAnimationFadeStrength  = 0.2
        
        SideMenuManager.menuFadeStatusBar = false
        SideMenuManager.menuWidth = view.frame.width*0.65
        SideMenuManager.menuShadowOpacity = 0
        
        /*
         SideMenuManager.menuPresentMode = .ViewSlideOut
         SideMenuManager.menuAllowPushOfSameClassTwice = true
         SideMenuManager.menuAllowPopIfPossible = false
         SideMenuManager.menuWidth = max(round(min(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) * 0.75), 240)
         SideMenuManager.menuAnimationPresentDuration = 0.35
         SideMenuManager.menuAnimationDismissDuration = 0.35
         SideMenuManager.menuAnimationFadeStrength = 0
         SideMenuManager.menuAnimationTransformScaleFactor = 1
         SideMenuManager.menuAnimationBackgroundColor = nil
         SideMenuManager.menuShadowOpacity = 0.5
         SideMenuManager.menuShadowColor = UIColor.blackColor()
         SideMenuManager.menuShadowRadius = 5
         //         SideMenuManager.menuLeftSwipeToDismissGesture: UIPanGestureRecognizer?
         //         SideMenuManager.menuRightSwipeToDismissGesture: UIPanGestureRecognizer?
         SideMenuManager.menuParallaxStrength = 0
         SideMenuManager.menuFadeStatusBar = true
         SideMenuManager.menuBlurEffectStyle = nil // Note: if you want cells in a UITableViewController menu to look good, make them a subclass of UITableViewVibrantCell!
         SideMenuManager.menuLeftNavigationController = navc
         SideMenuManager.menuRightNavigationController = nil
         //        SideMenuManager.menuAddScreenEdgePanGesturesToPresent(toView:UIView, forMenu:UIRectEdge? = nil) -> [UIScreenEdgePanGestureRecognizer]
         //         SideMenuManager.menuAddPanGestureToPresent(toView toView: UIView) -> UIPanGestureRecognizer
         */
    }
    
    
    @objc func leftBtnClick(){
        
        present(SideMenuManager.menuLeftNavigationController!, animated: true, completion: nil)
        
    }
    
    
}
